//
//  ViewController.m
//  设计模式
//
//  Created by 赛 Sir on 2023/10/29.
//

#import "ViewController.h"
#import "ZSModel.h"
#import "ZSAppPresenter.h"
#import "ZSAppViewModel.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <ZSModel *>*mutAry;

@property (nonatomic, strong) ZSAppPresenter *presenter;

@property (nonatomic, strong) ZSAppViewModel *viewModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *designMode = @"MVC";
    //designMode = @"MVP";
    designMode = @"MVVM";

    if([designMode isEqualToString:@"MVC"]) {
        // MVC
        [self loadData];
        [self.view addSubview:self.tableView];
    } else if ([designMode isEqualToString:@"MVP"]) {
        // MVP
        _presenter = [[ZSAppPresenter alloc] initWith:self];
        
    } else {
        //  MVVM
        _viewModel = [[ZSAppViewModel alloc] initWith:self];
    }
}

- (void)loadData {
    _mutAry = [NSMutableArray array];
    for (NSInteger i = 0; i < 20; i++) {
        ZSModel *news = [[ZSModel alloc] init];
        news.titleStr = [NSString stringWithFormat:@"news_title_%ld", i];
        [_mutAry addObject:news];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mutAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:@"cellStr"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellStr"];
        }
    cell.textLabel.text = _mutAry[indexPath.row].titleStr;
    
    
    
    return cell;
}

- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

@end
